package com.xlh.support.quiz.controller;

import com.xlh.common.Result;
import com.xlh.dto.user.UserInfoDTO;
import com.xlh.security.UserInfoHolder;
import com.xlh.service.user.UserService;
import com.xlh.support.quiz.po.QuizAnswerPO;
import com.xlh.support.quiz.po.QuizAssessPO;
import com.xlh.support.quiz.service.QuizService;
import com.xlh.support.quiz.vo.PerformanceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@Api("随堂提问")
@RestController
@RequestMapping("/quiz")
public class QuizController {

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Autowired
    private QuizService quizService;

    @Autowired
    private UserService userService;

    @ApiOperation("获取班级姓名")
    @GetMapping("{courseId}/students")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result queryStudentNames(@PathVariable("courseId") Long courseId) {
        List<UserInfoDTO> userInfoDTOS = userService.queryDefaultClassUser(courseId);
        return Result.success(userInfoDTOS);
    }

    /*-------   判断题接口   -------*/

    @ApiOperation("提问")
    @PostMapping("/{quizId}/ask")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result quiz(@PathVariable("quizId") Long quizId) {
        quizService.askQuiz(quizId);
        return Result.success();
    }

    @ApiOperation("回答提问")
    @PostMapping("/{quizId}/answer")
    @PreAuthorize("@permissionValidator.isStudent()")
    public Result answerQuiz(@PathVariable("quizId") Long quizId,
                             @RequestBody @Valid QuizAnswerPO quizAnswerPO) {
        quizService.answerQuiz(userInfoHolder.getUserId(), quizId, quizAnswerPO);
        return Result.success();
    }

    @ApiOperation("关闭提问")
    @PostMapping("/{quizId}/end-ask")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<PerformanceVO> endQuiz(@PathVariable("quizId") Long quizId) {
        PerformanceVO performanceVO = quizService.endQuiz(quizId);
        return Result.success(performanceVO);
    }

    /*-------   end   -------*/

    /*-------   简答题接口   -------*/

    @ApiOperation("评价提问")
    @PostMapping("/{quizId}/assess")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result assessQuiz(@PathVariable("quizId") Long quizId,
                             @RequestBody @Valid QuizAssessPO quizAssessPO) {
        quizService.assessQuiz(quizId, quizAssessPO);
        return Result.success();
    }

    /*-------   end   -------*/

}
